package redisson.rest;

import java.time.Duration;

import org.redisson.api.RLocalCachedMap;
import org.redisson.api.RedissonClient;
import org.redisson.api.options.LocalCachedMapOptions;
import org.redisson.api.options.LocalCachedMapOptions.EvictionPolicy;
import org.redisson.api.options.LocalCachedMapOptions.ReconnectionStrategy;
import org.redisson.api.options.LocalCachedMapOptions.SyncStrategy;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
//@RestController
@RequestMapping("/localCachedMapCache")
public class LocalCachedMapController implements SmartInitializingSingleton {

	@Autowired
	RedissonClient redissonClient;

	RLocalCachedMap<Object, Object> localCachedMap;

	@GetMapping("/test")
	public String next() {

		localCachedMap.fastPut("ccccccc", "cca");
		localCachedMap.clearLocalCache();

		return "true";
	}

	@GetMapping("/get")
	public Object get(@RequestParam String key) {
		return localCachedMap.get(key);
	}

	@GetMapping("/clearLocalCache")
	public Object clearLocalCache() {
		localCachedMap.clearLocalCache();
		return "true";
	}

	@Override
	public void afterSingletonsInstantiated() {
		// @formatter:off
		LocalCachedMapOptions<Object,Object> defaults = LocalCachedMapOptions.name("LocalCachedMap:Test:cc")
				.evictionPolicy(EvictionPolicy.NONE)
				.cacheSize(1000)
				.reconnectionStrategy(ReconnectionStrategy.CLEAR)
				.syncStrategy(SyncStrategy.UPDATE)
				.timeToLive(Duration.ofHours(8))
				.maxIdle(Duration.ofHours(1));
		// @formatter:on

		localCachedMap = redissonClient.getLocalCachedMap(defaults);

	}
}
